package completablefuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * @description: 有返回值
 * @author: WangPengPeng
 * @create: 2021-11-22 21:03
 **/
public class Demo2SupplierAsync {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "这是结果";
            }
        });

        String str = stringCompletableFuture.get();
        System.out.println("task result: " + str);

    }
}
